package com.pfizer.pfizerExpertForPad;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.reflect.Method;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.pfizer.DB.DataHelper;
import com.pfizer.Public.PublicFunc;

public class InitApp {
	private Context context;
	pfizerApplication pApplication;
	private String LoginPwd = "5fba362e58a5b71ba9e9f1d4d66d26bf";
	private String MainPwd = "17906aae65f0fb2143a44775b5a50e6c";

	public InitApp() {

	}

	public InitApp(Context _context) {
		context = _context;
		pApplication = (pfizerApplication) context.getApplicationContext();
	}

	public void SetApp(int _flag) {
		try {
			if (!pApplication.getIsInit()) {

				DataHelper dbHelper = new DataHelper(context);
				pApplication.setDataHelper(dbHelper);
				String prefsName = context.getPackageName() + "_preferences";
				SharedPreferences spSettings = context.getSharedPreferences(
						prefsName, Context.MODE_WORLD_WRITEABLE);
				String sTmp = "";
				int iTmp = 0;
				sTmp = spSettings.getString("etWebUrl",
						"http://www.testing.htm.tw");
				pApplication.setWebUrl(sTmp);
				sTmp = spSettings.getString("etLocalFilePath",
						"/sdcard/pfizer/");
				pApplication.setPfizerDir(sTmp);
				sTmp = spSettings.getString("lpVideoUpdIntervalTime", "6");
				iTmp = new Integer(sTmp).intValue();
				pApplication.setVideoUpdInterValTime(iTmp * 10);
				sTmp = spSettings.getString("lpHandoutUpdIntervalTime", "6");
				iTmp = new Integer(sTmp).intValue();
				pApplication.setHandoutUpdInterValTime(iTmp * 10);
				sTmp = spSettings.getString("lpSurveyUpdIntervalTime", "6");
				iTmp = new Integer(sTmp).intValue();
				pApplication.setSurveyUpdInterValTime(iTmp * 10);
				sTmp = spSettings.getString("lpNewsUpdIntervalTime", "6");
				iTmp = new Integer(sTmp).intValue();
				pApplication.setNewsUpdInterValTime(iTmp * 10);
				sTmp = spSettings.getString("lpMsgUpdIntervalTime", "4");
				iTmp = new Integer(sTmp).intValue();
				pApplication.setMsgUpdInterValTime(iTmp);
				
				pApplication.setObjID(getObjID());
				
				pApplication.setUserLoginProveCode(PublicFunc.MD5(LoginPwd
						+ pApplication.getObjFrom() + pApplication.getObjID()));
				
				pApplication.setMainProveCode(PublicFunc.MD5(pApplication
						.getObjID()
						+ MainPwd + pApplication.getObjFrom()));
				pApplication.setIsInit(true);
			}
			if (_flag == 1) {
				String prefsName = context.getPackageName() + "_preferences";
				SharedPreferences spSettings = context.getSharedPreferences(
						prefsName, Context.MODE_WORLD_WRITEABLE);
				String sTmp = "";
				int iTmp = 0;
				sTmp = spSettings.getString("etWebUrl",
						"http://www.testing.htm.tw/Pens_Api/");
				pApplication.setWebUrl(sTmp);
				sTmp = spSettings.getString("etLocalFilePath",
						"/sdcard/pfizer/");
				pApplication.setPfizerDir(sTmp);
				sTmp = spSettings.getString("lpVideoUpdIntervalTime", "6");
				iTmp = new Integer(sTmp).intValue();
				pApplication.setVideoUpdInterValTime(iTmp * 10);
				sTmp = spSettings.getString("lpHandoutUpdIntervalTime", "6");
				iTmp = new Integer(sTmp).intValue();
				pApplication.setHandoutUpdInterValTime(iTmp * 10);
				sTmp = spSettings.getString("lpSurveyUpdIntervalTime", "6");
				iTmp = new Integer(sTmp).intValue();
				pApplication.setSurveyUpdInterValTime(iTmp * 10);
				sTmp = spSettings.getString("lpNewsUpdIntervalTime", "6");
				iTmp = new Integer(sTmp).intValue();
				pApplication.setNewsUpdInterValTime(iTmp * 10);
				sTmp = spSettings.getString("lpMsgUpdIntervalTime", "4");
				iTmp = new Integer(sTmp).intValue();
				pApplication.setMsgUpdInterValTime(iTmp);
				pApplication.setIsInit(true);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private String getObjID()
	{
		String serialnum = null;
		try {
			Class<?> c = Class.forName("android.os.SystemProperties");
			Method get = c.getMethod("get", String.class, String.class);
			serialnum = (String) (get.invoke(c, "ril.serialnumber", "unknown"));
		} catch (Exception ignored) {
			serialnum = "unknown";
		}
		if(serialnum.equalsIgnoreCase("unknown"))
		{
			serialnum = getLocalMacAddress();
		}
		serialnum="0123456789";
		return serialnum;
	}

	public String getLocalMacAddress() {
		WifiManager wifi = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		String sTmp=info.getMacAddress();
		sTmp=sTmp.replace(":", "");
		return sTmp;
	}

	/**
	 * 获取CPU序列号
	 * 
	 * @return CPU序列号(16位) 读取失败为"0000000000000000"
	 */
	public static String getCPUSerial() {
		String str = "", strCPU = "", cpuAddress = "0000000000000000";
		try {
			// 读取CPU信息
			Process pp = Runtime.getRuntime().exec("cat /proc/cpuinfo");
			InputStreamReader ir = new InputStreamReader(pp.getInputStream());
			LineNumberReader input = new LineNumberReader(ir);
			// 查找CPU序列号
			for (int i = 1; i < 100; i++) {
				str = input.readLine();
				if (str != null) {
					// 查找到序列号所在行
					if (str.indexOf("Serial") > -1) {
						// 提取序列号
						strCPU = str.substring(str.indexOf(":") + 1, str
								.length());
						// 去空格
						cpuAddress = strCPU.trim();
						break;
					}
				} else {
					// 文件结尾
					break;
				}
			}
		} catch (IOException ex) {
			// 赋予默认值
			ex.printStackTrace();
		}
		return cpuAddress;
	}
}
